import router from '@/router';
import axios from 'axios';

axios.defaults.timeout = 5000;
axios.defaults.withCredentials=true;

//响应头
axios.defaults.headers.post['Content-Type']='application/x-www-form-urlencoded;charset=UTF-8';
//基础url
// axios.defaults.baseURL='http://yyxkserver.tshenqiu.top';
axios.defaults.baseURL='http://localhost:5000';
//拦截器
axios.interceptors.response.use(
  response =>{
    //response里的status为200
    if(response.status == 200){
      return Promise.resolve(response);
    }else{
      return Promise.reject(response);
    }
  },
  error =>{
    if(error.response.status){
      switch(error.response.status){
        case 401:
          router.replace({
            path:'/'
          });
          break;
        case 404:
          break;
      }
      return Promise.reject(error.response)
    }
  }
);

//get
export function get(url,params={}){
    return new Promise((resolve,reject) =>{
      axios.get(url,{params})
      .then(response =>{
        resolve(response.data)
      })
      .catch(err =>{
        reject(err)
      })
    })
}

//post
export function post(url,data={}){
  return new Promise((resolve,reject) =>{
    axios.post(url,data)
    .then(response =>{
      resolve(response.data)
    })
    .catch(err =>{
      reject(err)
    })
  })
}

